﻿using LeanApp.Bll;
using LiveChartsCore;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using RectangleGeometry = LiveChartsCore.SkiaSharpView.Drawing.Geometries.RectangleGeometry;

namespace LeanApp.View
{
    /// <summary>
    /// ControlCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class ControlCtrl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="model">数据实体</param>
        public ControlCtrl(string title, SpcBll model)
        {
            InitializeComponent();
            _model = model;
            _title = title;
            InitialChart();
        }

        /// <summary>
        /// 初始化图表
        /// </summary>
        private void InitialChart()
        {
            title.Text = _title;
            if (_model == null || _model.Source == null || _model.Source.Count <= 0)
            {
                return;
            }
            List<ISeries> series = new List<ISeries>();
            series.Add(
                new LineSeries<double>
                {
                    Values = _model.Source,
                    Fill = null,
                    Name = "数据记录",
                    Stroke = new SolidColorPaint(SKColors.Blue) { StrokeThickness = 4 },
                    GeometrySize = 8,
                    GeometryStroke = new SolidColorPaint(SKColors.Blue) { StrokeThickness = 4 }
                });

            series.Add(
                new LineSeries<double, RectangleGeometry>
                {
                    Values = _model.USLData,
                    Fill = null,
                    Name = "控制上限",
                    Stroke = new SolidColorPaint(SKColors.Red) { StrokeThickness = 4 },
                    GeometrySize = 8,
                    GeometryStroke = new SolidColorPaint(SKColors.Red) { StrokeThickness = 4 }
                });
            series.Add(
                new LineSeries<double, RectangleGeometry>
                {
                    Values = _model.LSLData,
                    Fill = null,
                    Name = "控制下限",
                    Stroke = new SolidColorPaint(SKColors.Red) { StrokeThickness = 4 },
                    GeometrySize = 8,
                    GeometryStroke = new SolidColorPaint(SKColors.Red) { StrokeThickness = 4 }
                });
            series.Add(
                new LineSeries<double, VariableSVGPathGeometry>
                {
                    Values = _model.AvgData,
                    Fill = null,
                    Name = "均值",
                    Stroke = new SolidColorPaint(SKColors.Yellow) { StrokeThickness = 4 },
                    GeometrySvg = SVGPoints.Cross,
                    GeometrySize = 8,
                    GeometryStroke = new SolidColorPaint(SKColors.Green) { StrokeThickness = 4 }
                });
            series.Add(
                new LineSeries<double, VariableSVGPathGeometry>
                {
                    Values = _model.SpecData,
                    Fill = null,
                    Name = "规格值",
                    Stroke = new SolidColorPaint(SKColors.Green) { StrokeThickness = 4 },
                    GeometrySvg = SVGPoints.Cross,
                    GeometrySize = 8,
                    GeometryStroke = new SolidColorPaint(SKColors.Yellow) { StrokeThickness = 4 }
                });
            chartSpcCtrl.Series = series;
            Axis xAxis = new Axis
            {
                Name = "序号",
                Labels = _model.SourceIndex.Select(a => a.ToString()).ToArray(),
            };
            chartSpcCtrl.XAxes = new Axis[] { xAxis };
        }

        /// <summary>
        /// 数据实体
        /// </summary>
        private SpcBll _model = new SpcBll();
        /// <summary>
        /// 标题
        /// </summary>
        private string _title;
    }
}
